package apresentacao.caminhoneiro;




import dao.caminhoneiro.AsyncTask_Buscar_Fretes_Raio;

import tcc.caminhoneiro.R;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;


public class Fragment_Buscar_Fretes_Raio extends Fragment{
	Spinner raioSpin;
	Button btBuscar;
	int novoRaio;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_busca_raio, null);
		inicializar(view);
		return view;
	}
	
    OnClickListener eventoBuscar = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AsyncTask_Buscar_Fretes_Raio async = new AsyncTask_Buscar_Fretes_Raio(novoRaio, getActivity());
			async.execute();
		}
	};
	
	private void inicializar(View view){
		raioSpin = (Spinner) view.findViewById(R.id.buscaRaioSpin);
		initRaioSpinner();
		
		btBuscar 		= (Button) view.findViewById(R.id.buscar_fretes_raio_btBuscar);
		btBuscar.setOnClickListener(eventoBuscar);
		
		OnItemSelectedListener eventoRaio = new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				novoRaio = 10000;
				switch(position){
				  case 1:{
					  novoRaio = 25000;
					  break;
				  }
				  case 2:{
					  novoRaio = 50000;
					  break;
				  }
				  case 3:{
					  novoRaio = 75000;
					  break;
				  }
				  case 4:{
					  novoRaio = 100000;
					  break;
				  }
				}
			
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
			
		};
		raioSpin.setOnItemSelectedListener(eventoRaio);
	}
	

	private void initRaioSpinner(){
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.item_raio,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    raioSpin.setAdapter(adapter);
	    raioSpin.setSelection(0);
	}

}
